class TalksController < ApplicationController
  include AuthenticatedSystemBrowse
  
  before_filter :login_required, :only => [ :new, :create, :edit, :update, :destroy ]
  
  def all
    @user = User.find_by_login(params[:user_id])
    respond_to do |format|
      format.html do
        @talks = Talk.paginate_by_sql(talks_all_sql + " ORDER BY created_at desc ", :page => params[:page])
        render :action => "all.rhtml"
      end
      format.rss  do
        @talks = Talk.find_by_sql(talks_all_sql + " ORDER BY created_at desc LIMIT 0 , 30 ")
        render :action => "all.rxml", :layout => false
      end
    end
  end
  
  def friends
    @user = User.find_by_login(params[:user_id])
    @friends = @user.friends_by_me
    
    user_ids = talks_str_of_friend_ids(@user)
    respond_to do |format|
      format.html do
        @talks = Talk.paginate_by_sql(talks_friends_sql(@user) + " ORDER BY created_at desc ", :page => params[:page])
        render :action => "friends.rhtml"
      end
      format.rss  do
        @talks = Talk.find_by_sql(talks_friends_sql(@user) + " ORDER BY created_at desc LIMIT 0 , 30 ")
        render :action => "friends.rxml", :layout => false
      end
    end
  end
  
  def index
    @user = User.find_by_login(params[:user_id])
    respond_to do |format|
      format.html do
        @talks = Talk.paginate_by_sql(talks_index_sql(@user) + " ORDER BY created_at desc ", :page => params[:page])
        render :action => "index.rhtml"
      end
      format.rss  do
        @talks = Talk.find_by_sql(talks_index_sql(@user) + " ORDER BY created_at desc LIMIT 0 , 30 ")
        render :action => "index.rxml", :layout => false
      end
    end
  end

  def show
    @talk = Talk.find(params[:id])
  end

  def edit
    @talk = current_user.talks.find(params[:id])
    @page_title = ' - 编辑状态'
  end

  def update
    @talk = current_user.talks.find(params[:id])
    @talk.update_attributes(params[:talk])
    
    respond_to do |format|
      format.html { redirect_to talk_url(current_user.login, @talk) }
      format.xml  { head 200 }
    end
  rescue ActiveRecord::RecordInvalid
    render :action => 'edit'
  end
  
  def new
    @talk = Talk.new
    @page_title = '新的状态'
  end
  
  def create
    Topic.transaction do
      @talk  = current_user.talks.build(params[:talk])
      @talk.save!
      current_user.talks << @talk
    end
    respond_to do |format|
      format.html { redirect_to_referer(talk_url(current_user.login, @talk)) }
      format.xml  { head :created, :location => formatted_talk_url(:user_id => current_user.login, :id => @talk, :format => :xml) }
    end
  rescue ActiveRecord::RecordInvalid
    render :action => 'new'
  end
  
  def destroy
    @talk = current_user.talks.find(params[:id])
    @talk.destroy
    redirect_to_referer talks_url(current_user.login)
  end
  
  protected
  # ORDER BY created_at desc LIMIT 0 , 30 
  def talks_all_sql
    if current_user.nil?
      sql = "SELECT talks.* FROM talks INNER JOIN (friendships, users) ON users.id = talks.user_id WHERE 
            (users.is_show_talks = 0) group by talks.id"
    else
      sql = "SELECT talks.* FROM talks INNER JOIN (friendships, users) ON users.id = talks.user_id WHERE 
            ((users.id = friendships.friend_id and friendships.user_id = #{current_user.id} and users.is_show_talks < 2) or 
            (users.id = #{current_user.id}) or 
            (users.is_show_talks = 0)) and
            (users.is_show_talks != 2 or users.id = #{current_user.id})
            group by talks.id"
    end
  end
  
  def talks_friends_sql(user)
    if current_user.nil?
      sql = "SELECT talks.* FROM talks INNER JOIN (friendships, users) ON users.id = talks.user_id WHERE 
            ((users.id = friendships.friend_id and friendships.user_id = #{user.id}) or 
            (users.id = #{user.id})) and 
            (users.is_show_talks = 0) group by talks.id"
    else
      if current_user == user
        sql = "SELECT talks.* FROM talks INNER JOIN (friendships, users) ON users.id = talks.user_id WHERE 
              (
                  (users.id = friendships.friend_id and friendships.user_id = #{current_user.id} and users.is_show_talks < 2) or 
                  (users.id = #{current_user.id})
              ) 
              group by talks.id"
      else
        sql = "SELECT talks.* FROM talks INNER JOIN (friendships, users) ON users.id = talks.user_id WHERE 
              (
                  (users.id = friendships.friend_id and friendships.user_id = #{user.id}) or 
                  (users.id = #{user.id})
              ) and 
              (
                  (users.is_show_talks = 0) or 
                  (users.is_show_talks = 1 and (users.id = friendships.friend_id and friendships.user_id = #{current_user.id}))
              ) 
              group by talks.id"
      end
    end
  end
  
  def talks_index_sql(user)
    if current_user == user
      sql = "SELECT talks.* FROM talks, users WHERE (talks.user_id = users.id AND users.id = #{user.id})"
    else
      if current_user.nil? || !user.is_friends_with?(current_user)
        sql = "SELECT talks.* FROM talks INNER JOIN users ON users.id = talks.user_id WHERE users.is_show_talks = 0 and users.id = #{user.id}"
      else
        sql = "SELECT talks.* FROM talks INNER JOIN users ON users.id = talks.user_id WHERE users.is_show_talks < 2 and users.id = #{user.id}"
      end
    end
  end
  
end
